package com.three.usermanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.three.core.entity.FrontUser;
import com.three.core.entity.User;
import com.three.core.vo.R;
import com.three.usermanagement.service.UserService;
import com.three.usermanagement.mapper.UserMapper;
import com.three.usermanagement.vo.UpdatePasswordVo;
import com.three.usermanagement.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public R<Page<User>> selectUserByPage(UserVo userVo) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        if (userVo.getUserName()!=null){
            queryWrapper.like("user_name",userVo.getUserName());
        }
        if (userVo.getPhonenumber()!=null){
            queryWrapper.like("phonenumber",userVo.getPhonenumber());
        }
        queryWrapper.eq("del_flag",0);
        queryWrapper.eq("status","0");
        Page<User> page = new Page<>(userVo.getCurrentPage(), userVo.getPageSize());
        Page<User> userPage = userMapper.selectPage(page, queryWrapper);
        return new R<Page<User>>(200,"查询成功",userPage);
    }

    @Override
    public R updateUser(User user) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId());
        int update = userMapper.update(user, queryWrapper);
        if (update>0){
            return new R(200,"修改成功",null);
        }else {
            return new R(500,"修改失败",null);
        }
    }

    @Override
    public R deleteUser(User user) {
        int i = userMapper.deleteUserById(user.getUserId());
        if (i>0){
            return new R(200,"删除成功",null);
        }else {
            return new R(500,"删除失败",null);
        }
    }

    @Override
    public R insert(User user) {
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        int insert = userMapper.insert(user);
        if (insert>0){
            return new R(200,"添加成功",null);
        }
        return new R(500,"添加失败",null);
    }

    @Override
    public R checkPass(UpdatePasswordVo updatePasswordVo) {
        //验证旧密码
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", updatePasswordVo.getUserId());
        User user = userMapper.selectOne(queryWrapper);
        if (!passwordEncoder.matches(updatePasswordVo.getPassOld(), user.getPassword())){
            return new R(500,"旧密码错误",null);
        }else {
            return new R(200,null,null);
        }
    }

    @Override
    public R updateUserPass(UpdatePasswordVo updatePasswordVo) {
        try {
            // 参数校验
            if (!updatePasswordVo.getPass().equals(updatePasswordVo.getCheckPass())) {
                return new R<>(500, "两次输入的密码不一致", null);
            }
            // 更新密码
            UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_id", updatePasswordVo.getUserId())
                    .set("password", passwordEncoder.encode(updatePasswordVo.getPass()));
            int result = userMapper.update(null, updateWrapper);
            if (result > 0) {
                return new R<>(200, "密码修改成功", null);
            } else {
                return new R<>(500, "密码修改失败", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new R<>(500, "系统错误", null);
        }
    }

}
